package com.qs.home.ui.VM;

import android.app.Application;
import android.databinding.ObservableBoolean;
import android.databinding.ObservableField;
import android.databinding.ObservableInt;
import android.support.annotation.NonNull;
import android.view.View;

import com.qs.base.utils.PhotoImgUtil;
import com.qs.home.entity.BackResultEntity;
import com.qs.home.entity.IdCardFrontResultEntity;
import com.qs.home.entity.TextResultEntity;

import java.io.File;

import me.goldze.mvvmhabit.base.BaseViewModel;
import me.goldze.mvvmhabit.binding.command.BindingAction;
import me.goldze.mvvmhabit.binding.command.BindingCommand;
import me.goldze.mvvmhabit.utils.ToastUtils;

/**
 * 银行卡识别结果
 *
 * @Author ltzz
 * @Date 2019/8/27
 */
public class BankCardResultsViewModel extends BaseViewModel {
    public BankCardResultsViewModel(@NonNull Application application) {
        super(application);
    }

    //识别数据
    public ObservableField<TextResultEntity> textResultOb = new ObservableField<>();

    //银行卡是否可编辑
    public ObservableBoolean isFocusable = new ObservableBoolean(false);
    //银行卡数据
    public ObservableField<BackResultEntity> bankResult = new ObservableField<>(new BackResultEntity());
    //银行卡历史数据
    public ObservableField<BackResultEntity> oldBankResult = new ObservableField<>(new BackResultEntity());
    //银行卡文本
    public String bankText;
    //文字是否编辑
    public ObservableInt textHind = new ObservableInt(View.GONE);
    //底部是否编辑
    public ObservableInt botHind = new ObservableInt(View.VISIBLE);

    //封装一个界面发生改变的观察者
    public UIChangeObservable uc = new UIChangeObservable();

    public class UIChangeObservable {
        //复制文本
        public ObservableField<String> copyTextUc = new ObservableField<>("");
        //翻译导出按钮
        public ObservableBoolean exportUc = new ObservableBoolean(false);
    }


    //返回按钮
    public BindingCommand backClick = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            finish();
        }
    });

    //保存按钮
    public BindingCommand saveClick = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            PhotoImgUtil.saveBitmap(getApplication(), PhotoImgUtil.pathToBitmap(textResultOb.get().getFilePath()), "image");
            ToastUtils.showShort("已保存至相册");
        }
    });

    //银行卡文本复制
    public BindingCommand bankTextClick = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            try {
                bankText = "银行：\n" + (bankResult.get().getBankName() == null ? "" : bankResult.get().getBankName()) +
                        "\n银行卡类型：\n" + (bankResult.get().getJsonRes().getResult().getBank_card_type() == null ? "" : bankResult.get().getJsonRes().getResult().getBank_card_type()) +
                        "\n卡号：\n" + (bankResult.get().getBankCardNumber() == null ? "" : bankResult.get().getBankCardNumber()) +
                        "\n有效期（如果有）：\n" + (bankResult.get().getJsonRes().getResult().getValid_date() == null ? "" : bankResult.get().getJsonRes().getResult().getValid_date());
            } catch (Exception e) {
                bankText = "";
            }
            uc.copyTextUc.set(bankText);
        }
    });
    //银行卡修改校对
    public BindingCommand bankModificationClick = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            oldBankResult.set(bankResult.get());
            isFocusable.set(!isFocusable.get());
            textHind.set(View.VISIBLE);
            botHind.set(View.INVISIBLE);
        }
    });
    //身份证修改保存
    public BindingCommand bankTextSaveClick = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            isFocusable.set(!isFocusable.get());
            textHind.set(View.GONE);
            botHind.set(View.VISIBLE);
        }
    });
    //取消
    public BindingCommand bankCloseClick = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            bankResult.set(oldBankResult.get());
            isFocusable.set(!isFocusable.get());
            textHind.set(View.GONE);
            botHind.set(View.VISIBLE);
        }
    });

    //导出按钮
    public BindingCommand exportClick = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            uc.exportUc.set(!uc.exportUc.get());
        }
    });
}
